<template>
  <div class="home-bg">
    <div class="home-header">
      <h1>杭州历史建筑信息交互平台</h1>
      <el-carousel height="260px" style="margin: 32px 0;">
        <el-carousel-item v-for="(img, idx) in carouselImages" :key="idx">
          <img :src="img" style="width: 100%; height: 100%; object-fit: cover; border-radius: 12px;" />
        </el-carousel-item>
      </el-carousel>
      <el-input v-model="searchKeyword" placeholder="请输入建筑名" style="width: 300px; margin-right: 16px;" @keyup.enter="handleSearch" />
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-select v-model="selectedCategory" placeholder="全部类别" style="width: 160px; margin-left: 16px;" @change="handleCategoryChange">
        <el-option label="全部类别" value="" />
        <el-option label="桥涵码头" value="桥涵码头" />
        <el-option label="亭台楼阙" value="亭台楼阙" />
      </el-select>
    </div>
    <div v-if="searchResult" class="search-result">
      <el-card class="spot-card">
        <img :src="searchResult.imageUrl" class="spot-img" />
        <div class="spot-title">{{ searchResult.name }}</div>
        <div class="spot-category">{{ searchResult.category }}</div>
        <div class="spot-desc">{{ searchResult.desc }}</div>
      </el-card>
      <el-button @click="clearSearch" style="margin-top: 16px;">返回全部</el-button>
    </div>
    <div v-else class="spot-list">
      <el-row :gutter="24">
        <el-col :span="6" v-for="spot in spotList" :key="spot.name">
          <el-card shadow="hover" class="spot-card" @click="openDetail(spot)">
            <img :src="spot.imageUrl" class="spot-img" @error="onImageError($event)" />
            <div class="spot-title">{{ spot.name }}</div>
            <div class="spot-category">{{ spot.category }}</div>
          </el-card>
        </el-col>
      </el-row>
      <div style="display: flex; justify-content: center; margin: 32px 0;">
        <el-pagination
          v-if="total > pageSize"
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page="currentPage"
          @current-change="handlePageChange"
        />
      </div>
    </div>
    <el-dialog v-model="detailVisible" title="建筑详情" width="500px" :close-on-click-modal="true">
      <template #default>
        <div v-if="detailSpot">
          <img :src="detailSpot.imageUrl" class="spot-img" style="margin-bottom: 12px;" />
          <div class="spot-title">{{ detailSpot.name }}</div>
          <div class="spot-category">{{ detailSpot.category }}</div>
          <div class="spot-desc" style="margin-top: 12px; min-height: 60px;">
            <template v-if="descLoading">加载中...</template>
            <template v-else>{{ detailSpot.desc }}</template>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const carouselImages = [
  '/your-image1.jpg',
  '/your-image2.jpg',
  '/your-image3.jpg'
];

function onImageError(e) {
  e.target.src = '/loading.png';
}

const spotList = ref([]);
const total = ref(0);
const pageSize = 12;
const currentPage = ref(1);
const selectedCategory = ref("");
const searchKeyword = ref('');
const searchResult = ref(null);

const detailVisible = ref(false);
const detailSpot = ref(null);
const descLoading = ref(false);

const fetchSpots = async () => {
  try {
    const res = await axios.get('/api/spot/list', {
      params: {
        page: currentPage.value,
        size: pageSize,
        category: selectedCategory.value
      }
    });
    spotList.value = res.data.list;
    total.value = res.data.total;
  } catch (e) {
    ElMessage.error('景点加载失败');
  }
};

const handleSearch = async () => {
  if (!searchKeyword.value) {
    ElMessage.warning('请输入建筑名');
    return;
  }
  try {
    const res = await axios.get('/api/spot/search', { params: { name: searchKeyword.value } });
    searchResult.value = res.data;
  } catch (e) {
    ElMessage.error('搜索失败');
  }
};

const clearSearch = () => {
  searchResult.value = null;
  searchKeyword.value = '';
  fetchSpots();
};

const openDetail = async (spot) => {
  detailSpot.value = { ...spot, desc: '' };
  detailVisible.value = true;
  descLoading.value = true;
  try {
    const res = await axios.get('/api/spot/desc', { params: { name: spot.name } });
    detailSpot.value.desc = res.data.desc;
  } catch (e) {
    detailSpot.value.desc = '简介加载失败';
  } finally {
    descLoading.value = false;
  }
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchSpots();
};

const handleCategoryChange = () => {
  currentPage.value = 1;
  fetchSpots();
};

onMounted(() => {
  fetchSpots();
});
</script>

<style>
.home-bg {
  min-height: 100vh;
  background: url('/bg-culture.png') center center/cover no-repeat;
  padding: 32px 0;
}
.home-header {
  text-align: center;
  margin-bottom: 24px;
}
.spot-list {
  max-width: 1200px;
  margin: 0 auto;
}
.spot-card {
  margin-bottom: 24px;
  min-height: 320px;
  transition: transform 0.3s ease;
}
.spot-card:hover {
  transform: translateY(-5px);
}
.spot-img {
  width: 100%;
  height: 180px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 12px;
}
.spot-title {
  font-weight: bold;
  font-size: 18px;
  margin-bottom: 8px;
}
.spot-category {
  color: #888;
  font-size: 14px;
  margin-bottom: 4px;
}
.spot-desc {
  color: #555;
  font-size: 15px;
  min-height: 60px;
  line-height: 1.4;
}
.search-result {
  max-width: 500px;
  margin: 0 auto 32px auto;
  text-align: center;
}
</style> 